package io.zjw.testblelib;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;


public class RecycleViewAdapter extends RecyclerView.Adapter {
    private View view;
    private Context mContext;
    private List<ScannedDevice> mlist;
    public RecycleViewAdapter(Context mContext,List<ScannedDevice> mlist) {
        this.mContext = mContext;
        this.mlist = mlist;
    }

    //找到需要导入的布局
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_scan_result, null);
        return new ViewHolder(view);

    }
    //加载相应的内容
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewholder=(ViewHolder)holder;
        viewholder.name.setText(mlist.get(position).getName());
        viewholder.mac.setText(mlist.get(position).getAddress());
        viewholder.rssi.setText(mlist.get(position).getRssi());
    }

    //手写的重置数据的方法
    public void setdata(ArrayList<ScannedDevice> list){
        if (list!=null&&list.size()>0){
            mlist.addAll(list);
        }

    }


    @Override
    public int getItemCount() {
        return mlist==null?0:mlist.size();
    }


    //定义的ViewHolder类继承RecyclerView.ViewHolder 并找到布局中的ID
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView mac;
        private TextView rssi;

        public ViewHolder(View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.tv_name);
            mac=itemView.findViewById(R.id.tv_mac);
            rssi=itemView.findViewById(R.id.tv_rssi);
        }
    }

}
